package cn.agiledata.bank.transaction.service;

import java.util.List;

import cn.agiledata.bank.hibernateBean.AccountBalance;
import cn.agiledata.bank.hibernateBean.SubAccount;
import cn.agiledata.bank.hibernateBean.SysParameter;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.common.dao.AccountTemplateInter;
import cn.agiledata.bank.common.util.Constant;
import com.vnv.infrastructure.logic.IPBHibernate3BaseMgr;

public class FillTransactionDetail extends IPBHibernate3BaseMgr implements FillTransactionDetailInterf{
	
	private AccountTemplateInter at ;
	
	
	TransactionDetail tdo ;	

	public void setTdo(TransactionDetail tdo) {
		this.tdo = tdo;
	}

	/* (non-Javadoc)
	 * @see cn.agiledata.bank.impl.fillTransactionDetailInterf#setAt(cn.agiledata.bank.common.dao.AccountTemplate)
	 */
	public void setAt(AccountTemplateInter at) {
		this.at = at;
	}

	/* (non-Javadoc)
	 * @see cn.agiledata.bank.impl.fillTransactionDetailInterf#fillAbundanceTransaction(cn.agiledata.bank.hibernateBean.Transaction, cn.agiledata.bank.hibernateBean.SysParameter)
	 */
	public List fillAbundanceTransaction(Transaction tran, SysParameter feeMode) {

		//获得所有内部账户
		List bankList = at.getInsideAccount();
/*		TransactionDetail tdo  = (TransactionDetailOperate)new TransactionDetailOperate()
		.new transactionDetailHandler().bind();*/
		//如果是银行收费且支付系统向客户收费
		if (feeMode.getParmValue().equals(Constant.FEE_MODE_FORFULLMONEY)){
			  /*填充交易明细数据，应该填充6条 */
			tdo.fillTransactionDetail(tran, bankList);
		}else //如果只是银行收费，支付系统不收费
			if(feeMode.getParmValue().equals(Constant.FEE_MODE_FORBANK)){
			tdo.fillTransactionDetailWithOnYSX(tran, bankList);
		}else//如果是免费操作
			if(feeMode.getParmValue().equals(Constant.FEE_MODE_NOMONEY)){
				tdo.fillTransactionDetailForFree(tran,bankList);
			}
		return bankList;
	}


	/* (non-Javadoc)
	 * @see cn.agiledata.bank.impl.fillTransactionDetailInterf#fillRemitTransaction(cn.agiledata.bank.hibernateBean.Transaction, cn.agiledata.bank.hibernateBean.SysParameter)
	 */
	public List fillRemitTransaction(Transaction tran, SysParameter withdrawFee) {
		List bankList = at.getInsideAccount();

		//用户提现多少，就实际汇给他多少，手续费另外出
		if (withdrawFee.getParmValue().equals(Constant.WITHDRAW_FEE_FRIST)){
			tdo.fillGainTransaction(tran,bankList);
		}else{
			tdo.fillGainTransacionPatternT2(tran,bankList);
		}
		return bankList;
	}	

	/* (non-Javadoc)
	 * @see cn.agiledata.bank.impl.fillTransactionDetailInterf#fillDownSiteInsertTransaction(cn.agiledata.bank.hibernateBean.Transaction)
	 */
	public List fillDownSiteInsertTransaction(Transaction tran) {
		List bankList = at.getInsideAccount();
		
		tdo.fillDepositByDownSite(tran,bankList,Constant.INITIAL_ACCOUNT_DOWNSITE);
		return bankList;
	}	
	
	/* (non-Javadoc)
	 * @see cn.agiledata.bank.impl.fillTransactionDetailInterf#fillTransactionBussiness(cn.agiledata.bank.hibernateBean.Transaction)
	 */
	public List fillTransactionBussiness(Transaction tran){

		List bankList = at.getInsideAccount();
		SubAccount credit = tran.getCredit();
		SubAccount debit = tran.getDebit();
		Double amount = tran.getAmount();
		Double returnAmount = tran.getReturnAmount();
		tran.setCredit((SubAccount)
				bankList.get(Integer.parseInt(Constant.INITIAL_ACCOUNT_ASSURE)-1));
		
		tdo.fillPayTransaction(tran,bankList);
		//REMARK: RETURN MONEY (部分退款) 担保账户资金(交易金额减去退款退掉金额)到收款方账户
		if (returnAmount != null && !returnAmount.equals(amount)) {
			tran.setDebit(credit);
			tran.setAmount(new Double(amount.doubleValue()+returnAmount.doubleValue()));
			tran.setReturnAmount(null);
			tdo.fillPayTransaction(tran,bankList);
			tran.setDebit(debit);
			tran.setAmount(amount);
			tran.setReturnAmount(returnAmount);
		}
		//添加lockAccount
		double lockAcocunt = 
			tran.getAmount().doubleValue();
		AccountBalance ab = credit.getAccountBalance();
		Double lockAmount=ab.getLockAmount();
		if(lockAmount==null){
			lockAmount=new Double(0);
			credit.getAccountBalance().setLockAmount(new Double(lockAmount.doubleValue()+lockAcocunt));
		}else{
			credit.getAccountBalance().setLockAmount(new Double(ab.getLockAmount().doubleValue()+lockAcocunt));
		}
		tran.setCredit(credit);
		return bankList;
	}
	
	/* (non-Javadoc)
	 * @see cn.agiledata.bank.impl.fillTransactionDetailInterf#fillTransactionBussinessSave(cn.agiledata.bank.hibernateBean.Transaction)
	 */
	public List fillTransactionBussinessSave(Transaction tran){

		List bankList = at.getInsideAccount();
		SubAccount debit = tran.getDebit();
		Double debitFee = tran.getDebitFee();
		tran.setDebit((SubAccount)
				bankList.get(Integer.parseInt(Constant.INITIAL_ACCOUNT_ASSURE)-1));
		
		tdo.fillDepositByDownSite(tran,bankList,Constant.INITIAL_ACCOUNT_ASSURE);
		//删除lockAccount
		double lockAcocunt = 
			tran.getAmount().doubleValue();
		AccountBalance ab = tran.getCredit().getAccountBalance();
		tran.getCredit().getAccountBalance().setLockAmount(new Double(ab.getLockAmount().doubleValue()-lockAcocunt));
		
		tran.setDebit(debit);
		tran.setDebitFee(debitFee);
		return bankList;		
	}
	
	/* (non-Javadoc)
	 * @see cn.agiledata.bank.impl.fillTransactionDetailInterf#fillTransactionWithOutAssure(cn.agiledata.bank.hibernateBean.Transaction)
	 */
	public List fillTransactionWithOutAssure(Transaction tran){

		List bankList = at.getInsideAccount();
		SubAccount debit = tran.getDebit();
		SubAccount credit = tran.getCredit();
		SubAccount trmpAccount = (SubAccount)bankList.get(6);
		//首先是付款过程
		tran.setCredit(trmpAccount);
		tdo.fillPayTransaction(tran,bankList);
		Double debitFee = tran.getDebitFee();
		//其次是存款工程
		tran.setDebit(trmpAccount);
		tran.setCredit(credit);
		tran.setOperate(Constant.MODEL_TYPE_GATHERING);
		tran.setTransStatus(Constant.TRANS_STATUS_TRANSACTIONSUCCESS);
		tdo.fillDepositByDownSite(tran,bankList,Constant.INITIAL_ACCOUNT_NOASSURE);
		tran.setDebit(debit);
		tran.setDebitFee(debitFee);
		return bankList;
	}

	public List fillTransactionWithOutAssureStep1(Transaction tran) {
		List bankList = at.getInsideAccount();
		SubAccount trmpAccount = (SubAccount)bankList.get(6);
		//首先是付款过程
		tran.setCredit(trmpAccount);
		tdo.fillPayTransaction(tran,bankList);
		return bankList;
	}

	public List fillTransactionWithOutAssureStep2(Transaction tran) {
		List bankList = at.getInsideAccount();
		SubAccount debit = tran.getDebit();
		SubAccount trmpAccount = (SubAccount)bankList.get(6);
		// TODO Auto-generated method stub
		tran.setDebit(trmpAccount);
		tran.setOperate(Constant.MODEL_TYPE_GATHERING);
		tran.setTransStatus(Constant.TRANS_STATUS_TRANSACTIONSUCCESS);
		tdo.fillDepositByDownSite(tran,bankList,Constant.INITIAL_ACCOUNT_NOASSURE);
		tran.setDebit(debit);
		return bankList;
	}

}